home *** CD-ROM | disk | FTP | other *** search
- #include <MacTypes.h>
- #include <Resources.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Controls.h>
- #include <MacWindows.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Devices.h>
- #include <Events.h>
- #include <Scrap.h>
- #include <ToolUtils.h>
- #include <LowMem.h>
- #include <MacMemory.h>
- #include <Segload.h>
- #include <AppleEvents.h>
- #include <Files.h>
- #include <OSUtils.h>
- #include <DiskInit.h>
- #include <Packages.h>
- #include <Traps.h>
- #include <FixMath.h>
- #include <NumberFormatting.h>
- #include <TextUtils.h>
- #include <ColorPicker.h>
- #include <Processes.h>
- #include <Gestalt.h>
- #include <Palettes.h>
- #include <Sound.h>
- #include <fp.h>
-
- #include <ImageCompression.h>
- #include <QuickTimeComponents.h>
-
- #include <string.h>
- #include <stdio.h>
-
- #include "DemoGraphicsImporters.h"
-
- //
- // Typedefs
- //
- typedef struct {
- WindowRecord docWindow;
- FSSpec docFSSpec;
- GraphicsImportComponent docGrip;
- GraphicsImportComponent docGripBackground;
- MatrixRecord docMatrix;
- TEHandle docText;
- short docDemo;
- Rect docTranslateBounds; // used with iTranslate
- Fixed docAngle; // used with iRotate
- Point docFourPoints[4]; // labelled clockwise, top-left first
- Boolean docCodeVisible;
- Boolean docMatrixVisible;
- Boolean docFourPointsVisible;
- long docTransferModeMenuItem;
- long docTransferMode;
- RGBColor docOpColor;
- } DocumentRecord, *DocumentPeek;
-
- //
- // Macros
- //
- #define HiWrd(aLong) (((aLong) >> 16) & 0xFFFF)
- #define LoWrd(aLong) ((aLong) & 0xFFFF)
-
- #define TopLeft(aRect) (* (Point *) &(aRect).top)
- #define BotRight(aRect) (* (Point *) &(aRect).bottom)
-
- #define RectWidth(aRect) ((long)((aRect).right - (aRect).left))
- #define RectHeight(aRect) ((long)((aRect).bottom - (aRect).top))
-
- #define IntToFixed(i) ((Fixed)(i) << 16)
-
- //
- // Application Globals
- //
- Boolean gInBackground;
- Boolean gExitFlag = false;
-
- //
- // Function Prototypes
- //
-
- void FitRectIntoRect( const Rect *r, const Rect *s, Rect *t );
- void PolyToPolyMatrix(const Fixed *source, const Fixed *dest, MatrixRecord *map);
-
- void RectToPoly( Rect *r, Fixed poly[9] );
- void FourPointsToPoly( Point fourPoints[4], Fixed poly[9] );
- void RectToFourPoints( Rect *r, Point fourPoints[4] );
-
- void DrawGraphicInBox( DocumentPeek theDocument, Rect *drawRect );
- void DrawGraphicRotated( DocumentPeek theDocument, Rect *drawRect );
- void DrawGraphicDistorted( DocumentPeek theDocument, Rect *drawRect );
- void DrawFourPoints( DocumentPeek theDocument, Point p[4], short drawMode, Boolean blobs );
-
- Fixed WorkOutAngle( Point p, Point c );
- short DistanceBetweenPoints( Point a, Point b );
- void DoTranslateClick( DocumentPeek theDocument, EventRecord *event );
- void DoRotateClick( DocumentPeek theDocument, EventRecord *event );
- void DoDistortClick( DocumentPeek theDocument, EventRecord *event );
-
- void DrawBackground( DocumentPeek theDocument );
- OSErr DrawGraphic( DocumentPeek theDocument );
- OSErr DrawMatrix( DocumentPeek theDocument );
-
- OSErr SetUpDemoText( DocumentPeek theDocument );
- void SetCodeVisible( DocumentPeek theDocument, Boolean visible );
- void SetMatrixVisible( DocumentPeek theDocument, Boolean visible );
- void SetFourPointsVisible( DocumentPeek theDocument, Boolean visible );
-
- void RedrawAfterMatrixChange( DocumentPeek theDocument );
- OSErr DrawWindow( WindowPtr window );
- void GetDocumentRects( DocumentPeek doc, Rect *outTextRect, Rect *outDrawRect, Rect *outMatrixRect, Rect *outControlsRect );
- void ResetDemoMatrix( DocumentPeek theDocument );
- void DoMatrixMenu( WindowPtr window, short item );
- void DoCompositionMenu( WindowPtr window, short item );
- void DoTransferModeMenu( WindowPtr window, short item );
- OSErr MakeDocumentControls( DocumentPeek doc, Rect *controlsRect );
-
-
- OSErr OpenFSSpec( FSSpec *inFSSpec );
- void DoOpen( void );
- void DoCloseWindow(WindowPtr window);
- void AdjustMenus(void);
- void DoMenuCommand(long menuResult);
- unsigned long GetSleep(void);
- void DoKeyDown(EventRecord *event);
- void DoContentClick(WindowPtr window,EventRecord *event);
- void DoActivate(WindowPtr window, Boolean becomingActive);
- void DoUpdate(WindowPtr window);
- void DoZoomWindow(WindowPtr window,short part);
- void DoGrowWindow(WindowPtr window,EventRecord *event);
- void DoIdle();
- Boolean ShiftKeyIsDown();
- void GetGlobalMouse(Point *mouse);
- void AdjustCursor(Point mouse,RgnHandle region);
- void DoEvent(EventRecord *event);
- void DoAppleEvent(EventRecord theEvent);
- pascal OSErr DoAEOpenApp(const AppleEvent * message,const AppleEvent * reply,long refcon );
- pascal OSErr DoAEOpenDocument(const AppleEvent * message, const AppleEvent * reply, long refcon );
- pascal OSErr DoAEQuit(const AppleEvent * message,const AppleEvent * reply,long refcon );
- pascal OSErr DoAEPrintDocuments(const AppleEvent * message, AppleEvent * reply, long refcon );
- void InitAppleEvents(void);
- void EventLoop(void);
- void Terminate( void );
- OSErr Initialize( void );
- void main(void);
-
-
- //
- // FitRectIntoRect
- //
- void FitRectIntoRect( const Rect *r, const Rect *s, Rect *t )
- {
- long r_width = RectWidth(*r);
- long r_height = RectHeight(*r);
- long s_width = RectWidth(*s);
- long s_height = RectHeight(*s);
- if( r_width < s_width && r_height < s_height ) {
- short s_hmid = ( s->left + s->right ) / 2;
- short s_vmid = ( s->top + s->bottom ) / 2;
- short t_half_width = r_width / 2;
- short t_half_height = r_height / 2;
-
- t->top = s_vmid - t_half_height;
- t->bottom = s_vmid + t_half_height;
- t->left = s_hmid - t_half_width;
- t->right = s_hmid + t_half_width;
- }
- else if( r_width * s_height > r_height * s_width ) {
- short s_vmid = ( s->top + s->bottom ) / 2;
- short t_half_height = FixMul( r_height, FixDiv( s_width, r_width ) ) / 2;
-
- t->top = s_vmid - t_half_height;
- t->bottom = s_vmid + t_half_height;
- t->left = s->left;
- t->right = s->right;
- } else {
- short s_hmid = ( s->left + s->right ) / 2;
- short t_half_width = FixMul( r_width, FixDiv( s_height, r_height ) ) / 2;
-
- t->left = s_hmid - t_half_width;
- t->right = s_hmid + t_half_width;
- t->top = s->top;
- t->bottom = s->bottom;
- }
- }
-
- //
- // RectToPoly
- //
- void RectToPoly( Rect *r, Fixed poly[9] )
- {
- poly[0] = 4;
- poly[1] = IntToFixed( r->left );
- poly[2] = IntToFixed( r->top );
- poly[3] = IntToFixed( r->right );
- poly[4] = IntToFixed( r->top );
- poly[5] = IntToFixed( r->right );
- poly[6] = IntToFixed( r->bottom );
- poly[7] = IntToFixed( r->left );
- poly[8] = IntToFixed( r->bottom );
- }
-
- //
- // FourPointsToPoly
- //
- void FourPointsToPoly( Point fourPoints[4], Fixed poly[9] )
- {
- poly[0] = 4;
- poly[1] = IntToFixed( fourPoints[0].h );
- poly[2] = IntToFixed( fourPoints[0].v );
- poly[3] = IntToFixed( fourPoints[1].h );
- poly[4] = IntToFixed( fourPoints[1].v );
- poly[5] = IntToFixed( fourPoints[2].h );
- poly[6] = IntToFixed( fourPoints[2].v );
- poly[7] = IntToFixed( fourPoints[3].h );
- poly[8] = IntToFixed( fourPoints[3].v );
- }
-
- //
- // RectToFourPoints
- //
- void RectToFourPoints( Rect *r, Point fourPoints[4] )
- {
- fourPoints[0].h = r->left;
- fourPoints[0].v = r->top;
- fourPoints[1].h = r->right;
- fourPoints[1].v = r->top;
- fourPoints[2].h = r->right;
- fourPoints[2].v = r->bottom;
- fourPoints[3].h = r->left;
- fourPoints[3].v = r->bottom;
- }
-
-
-
- //
- // DrawGraphicInBox
- //
- void DrawGraphicInBox( DocumentPeek theDocument, Rect *drawRect )
- {
- GraphicsImportComponent gi = theDocument->docGrip;
- Rect naturalBounds, bounds;
- MatrixRecord matrix;
- Point naturalBoundsAsPoints[4];
-
- GraphicsImportGetNaturalBounds( gi, &naturalBounds );
- bounds = theDocument->docTranslateBounds;
-
- GraphicsImportSetBoundsRect( gi, &bounds );
- GraphicsImportDraw( gi );
-
- GraphicsImportGetMatrix( gi, &matrix );
- theDocument->docMatrix = matrix;
-
- if( theDocument->docFourPointsVisible ) {
- RectToFourPoints( &naturalBounds, naturalBoundsAsPoints );
- TransformPoints( &matrix, naturalBoundsAsPoints, 4 );
- DrawFourPoints( theDocument, naturalBoundsAsPoints, kDrawSolid, kHideBlobs );
- }
- }
-
- //
- // DrawGraphicRotated
- //
- void DrawGraphicRotated( DocumentPeek theDocument, Rect *drawRect )
- {
- GraphicsImportComponent gi = theDocument->docGrip;
- Rect naturalBounds, bounds;
- MatrixRecord matrix;
- Fixed angle, x, y;
- Point naturalBoundsAsPoints[4];
-
- GraphicsImportGetNaturalBounds( gi, &naturalBounds );
- FitRectIntoRect( &naturalBounds, drawRect, &bounds );
-
- angle = theDocument->docAngle;
- x = IntToFixed((bounds.right + bounds.left) / 2);
- y = IntToFixed((bounds.bottom + bounds.top) / 2);
-
- RectMatrix( &matrix, &naturalBounds, &bounds );
- RotateMatrix( &matrix, angle, x, y );
- GraphicsImportSetMatrix( gi, &matrix );
- GraphicsImportDraw( gi );
-
- theDocument->docMatrix = matrix;
-
- if( theDocument->docFourPointsVisible ) {
- RectToFourPoints( &naturalBounds, naturalBoundsAsPoints );
- TransformPoints( &matrix, naturalBoundsAsPoints, 4 );
- DrawFourPoints( theDocument, naturalBoundsAsPoints, kDrawSolid, kHideBlobs );
- }
- }
-
- //
- // DrawGraphicDistorted
- //
- void DrawGraphicDistorted( DocumentPeek theDocument, Rect *drawRect )
- {
- GraphicsImportComponent gi = theDocument->docGrip;
- Rect naturalBounds;
- MatrixRecord matrix;
- Fixed fromPoly[9];
- Fixed toPoly[9];
-
- GraphicsImportGetNaturalBounds( gi, &naturalBounds );
-
- RectToPoly( &naturalBounds, fromPoly );
- FourPointsToPoly( theDocument->docFourPoints, toPoly );
-
- PolyToPolyMatrix( fromPoly, toPoly, &matrix );
- GraphicsImportSetMatrix( gi, &matrix );
- GraphicsImportDraw( gi );
-
- theDocument->docMatrix = matrix;
-
- if( theDocument->docFourPointsVisible )
- DrawFourPoints( theDocument, theDocument->docFourPoints, kDrawSolid, kShowBlobs );
- }
-
- //
- // DrawFourPoints
- //
- void DrawFourPoints( DocumentPeek theDocument, Point p[4], short drawMode, Boolean blobs )
- {
- #pragma unused( theDocument )
- const short thick = 2;
- const short blob = kBlobRadius;
- const RGBColor white = { 0xffff, 0xffff, 0xffff };
- const RGBColor black = { 0x0000, 0x0000, 0x0000 };
- const RGBColor fire = { 0xffff, 0x6666, 0x0000 };
- const RGBColor dirt = { 0x6666, 0x3333, 0x2222 };
- short i;
- Boolean erase = (drawMode == kEraseSolid || drawMode == kEraseRubber);
- Boolean rubber_xor;
-
- rubber_xor =
- (drawMode == kDrawRubber || drawMode == kEraseRubber);
-
- PenSize( thick*2, thick*2 );
- if( rubber_xor ) {
- PenMode( patXor );
- RGBForeColor( &dirt );
- } else {
- RGBForeColor( erase ? &white : &dirt );
- }
-
- MoveTo( p[3].h - thick, p[3].v - thick );
- for( i = 0; i < 4; i++ ) {
- LineTo( p[i].h - thick, p[i].v - thick );
- }
-
- if( blobs ) {
- if( rubber_xor ) {
- RGBForeColor( &dirt );
- } else {
- RGBForeColor( erase ? &white : &fire );
- }
-
- for( i = 0; i < 4; i++ ) {
- Rect r;
- MacSetRect( &r, p[i].h - blob, p[i].v - blob, p[i].h + blob, p[i].v + blob );
- PaintOval( &r );
- }
- }
-
- PenNormal();
- RGBForeColor( &black );
- }
-
- //
- // DoTranslateClick
- //
- void DoTranslateClick( DocumentPeek theDocument, EventRecord *event )
- {
- GraphicsImportComponent gi = theDocument->docGrip;
- Rect naturalBounds, bounds, startBounds;
- Point startWhere = event->where;
- Point oldWhere = {-9999,-9999}, where;
- Boolean wasDrawn = false;
- Point fourPoints[4];
-
- GlobalToLocal( &startWhere );
-
- GraphicsImportGetNaturalBounds( gi, &naturalBounds );
- bounds = theDocument->docTranslateBounds;
- startBounds = bounds;
-
- RectToFourPoints( &bounds, fourPoints );
- if( theDocument->docFourPointsVisible )
- DrawFourPoints( theDocument, fourPoints, kEraseSolid, kHideBlobs );
-
- do {
- GetMouse( &where );
-
- if( where.h != oldWhere.h || where.v != oldWhere.v )
- {
- if( wasDrawn )
- DrawFourPoints( theDocument, fourPoints, kEraseRubber, kHideBlobs );
-
- bounds = startBounds;
- OffsetRect( &bounds, where.h - startWhere.h, where.v - startWhere.v );
-
- RectToFourPoints( &bounds, fourPoints );
-
- DrawFourPoints( theDocument, fourPoints, kDrawRubber, kHideBlobs );
- wasDrawn = true;
-
- oldWhere = where;
- }
- } while( StillDown() );
-
- if( wasDrawn )
- DrawFourPoints( theDocument, fourPoints, kEraseSolid, kHideBlobs );
-
- theDocument->docTranslateBounds = bounds;
-
- RedrawAfterMatrixChange( theDocument );
- }
-
- //
- // WorkOutAngle
- //
- Fixed WorkOutAngle( Point p, Point c )
- {
- long x = p.h - c.h;
- long y = p.v - c.v;
- return FixMul( FixATan2( y, -x ), FixDiv( IntToFixed(180), X2Fix(3.1415) ) );
- }
-
- //
- // DoRotateClick
- //
- void DoRotateClick( DocumentPeek theDocument, EventRecord *event )
- {
- GraphicsImportComponent gi = theDocument->docGrip;
- MatrixRecord matrix;
- Rect drawRect;
- Rect naturalBounds, bounds;
- Fixed baseAngle, angle, x, y;
- Point startWhere = event->where;
- Point oldWhere = {-9999,-9999}, where;
- Boolean wasDrawn = false;
- Point naturalBoundsAsPoints[4];
- Point centerOfBounds;
-
- GlobalToLocal( &startWhere );
- GetDocumentRects( theDocument, nil, &drawRect, nil, nil );
-
- angle = theDocument->docAngle;
-
- GraphicsImportGetNaturalBounds( gi, &naturalBounds );
- FitRectIntoRect( &naturalBounds, &drawRect, &bounds );
- centerOfBounds.h = (bounds.right + bounds.left) / 2;
- centerOfBounds.v = (bounds.bottom + bounds.top) / 2;
- x = IntToFixed(centerOfBounds.h);
- y = IntToFixed(centerOfBounds.v);
-
- baseAngle = angle - WorkOutAngle( startWhere, centerOfBounds );
-
- RectToFourPoints( &naturalBounds, naturalBoundsAsPoints );
- RectMatrix( &matrix, &naturalBounds, &bounds );
- RotateMatrix( &matrix, angle, x, y );
- TransformPoints( &matrix, naturalBoundsAsPoints, 4 );
-
- if( theDocument->docFourPointsVisible )
- DrawFourPoints( theDocument, naturalBoundsAsPoints, kEraseSolid, kShowBlobs );
-
- do {
- GetMouse( &where );
-
- if( where.h != oldWhere.h || where.v != oldWhere.v )
- {
- angle = baseAngle + WorkOutAngle( where, centerOfBounds );
- if( ShiftKeyIsDown() ) {
- const Fixed moduloAngle = IntToFixed( 45 );
- angle = ( (angle + moduloAngle/2) / moduloAngle ) * moduloAngle;
- }
-
- oldWhere = where;
-
- if( wasDrawn )
- DrawFourPoints( theDocument, naturalBoundsAsPoints, kEraseRubber, kShowBlobs );
-
- RectToFourPoints( &naturalBounds, naturalBoundsAsPoints );
- RectMatrix( &matrix, &naturalBounds, &bounds );
- RotateMatrix( &matrix, angle, x, y );
- TransformPoints( &matrix, naturalBoundsAsPoints, 4 );
-
- DrawFourPoints( theDocument, naturalBoundsAsPoints, kDrawRubber, kShowBlobs );
- wasDrawn = true;
- }
- } while( StillDown() );
-
- if( wasDrawn )
- DrawFourPoints( theDocument, naturalBoundsAsPoints, kEraseSolid, kShowBlobs );
-
- theDocument->docAngle = angle;
-
- RedrawAfterMatrixChange( theDocument );
- }
-
- //
- // DistanceBetweenPoints
- //
- short DistanceBetweenPoints( Point a, Point b )
- {
- long h = a.h - b.h;
- long v = a.v - b.v;
- return FracSqrt( h*h + v*v ) >> 15;
- }
-
- //
- // DoDistortClick
- //
- void DoDistortClick( DocumentPeek theDocument, EventRecord *event )
- {
- GraphicsImportComponent gi = theDocument->docGrip;
- Point startWhere = event->where;
- Point oldWhere = {-9999,-9999}, where;
- Boolean wasDrawn = false;
- Point fourPoints[4], originalLoc;
- short whichPoint, whichPointDist, i;
-
- GlobalToLocal( &startWhere );
-
- BlockMove( theDocument->docFourPoints, fourPoints, sizeof(fourPoints) );
-
- whichPoint = -1;
- whichPointDist = 30000;
- for( i = 0; i < 4; i++ ) {
- short dist = DistanceBetweenPoints( fourPoints[i], startWhere );
- if( dist < kBlobRadius && dist < whichPointDist ) {
- whichPoint = i;
- whichPointDist = dist;
- }
- }
- if( -1 == whichPoint )
- return;
- originalLoc = fourPoints[whichPoint];
-
- if( theDocument->docFourPointsVisible )
- DrawFourPoints( theDocument, fourPoints, kEraseSolid, kShowBlobs );
-
- do {
- GetMouse( &where );
-
- if( where.h != oldWhere.h || where.v != oldWhere.v )
- {
- if( wasDrawn )
- DrawFourPoints( theDocument, fourPoints, kEraseRubber, kShowBlobs );
-
- fourPoints[ whichPoint ].h = originalLoc.h + where.h - startWhere.h;
- fourPoints[ whichPoint ].v = originalLoc.v + where.v - startWhere.v;
-
- oldWhere = where;
-
- DrawFourPoints( theDocument, fourPoints, kDrawRubber, kShowBlobs );
- wasDrawn = true;
- }
- } while( StillDown() );
-
- BlockMove( fourPoints, theDocument->docFourPoints, sizeof(fourPoints) );
-
- if( wasDrawn )
- DrawFourPoints( theDocument, fourPoints, kEraseSolid, kShowBlobs );
-
- RedrawAfterMatrixChange( theDocument );
- }
-
- //
- // DrawBackground( DocumentPeek theDocument )
- //
- void DrawBackground( DocumentPeek theDocument )
- {
- if( theDocument->docGripBackground ) {
- GraphicsImportDraw( theDocument->docGripBackground );
- }
- }
-
- //
- // DrawGraphic
- //
- OSErr DrawGraphic( DocumentPeek theDocument )
- {
- Rect drawRect;
- GetDocumentRects( theDocument, nil, &drawRect, nil, nil );
-
- switch( theDocument->docDemo ) {
- case iTranslate:
- DrawGraphicInBox( theDocument, &drawRect );
- break;
- case iRotate:
- DrawGraphicRotated( theDocument, &drawRect );
- break;
- case iDistort:
- DrawGraphicDistorted( theDocument, &drawRect );
- break;
- }
-
- return noErr;
- }
-
- //
- // SetUpDemoText
- //
- OSErr SetUpDemoText( DocumentPeek theDocument )
- {
- Rect textRect;
- Ptr textPtr = "-";
- long textLength;
- Boolean hideTransferMode = (theDocument->docTransferMode == srcCopy + ditherCopy);
-
- switch( theDocument->docDemo ) {
- case iTranslate:
- if( hideTransferMode )
- textPtr =
- "GetGraphicsImporterForFile( &fileSpec, &gi );\r"
- "GraphicsImportSetBoundsRect( gi, &bounds );\r"
- "GraphicsImportDraw( gi );\r"
- "CloseComponent( gi );\r";
- else
- textPtr =
- "GetGraphicsImporterForFile( &fileSpec, &gi );\r"
- "GraphicsImportSetBoundsRect( gi, &bounds );\r"
- "GraphicsImportSetGraphicsMode( gi, mode, color );\r"
- "GraphicsImportDraw( gi );\r"
- "CloseComponent( gi );\r";
- break;
- case iRotate:
- if( hideTransferMode )
- textPtr =
- "GraphicsImportGetNaturalBounds( gi, &natBounds );\r"
- "RectMatrix( &matrix, &natBounds, &bounds );\r"
- "RotateMatrix( &matrix, angle, x, y );\r"
- "GraphicsImportSetMatrix( gi, &matrix );\r"
- "GraphicsImportDraw( gi );\r";
- else
- textPtr =
- "GraphicsImportGetNaturalBounds( gi, &natBounds );\r"
- "RectMatrix( &matrix, &natBounds, &bounds );\r"
- "RotateMatrix( &matrix, angle, x, y );\r"
- "GraphicsImportSetMatrix( gi, &matrix );\r"
- "GraphicsImportSetGraphicsMode( gi, mode, color );\r"
- "GraphicsImportDraw( gi );\r";
- break;
- case iDistort:
- if( hideTransferMode )
- textPtr =
- "PolyToPolyMatrix( fromPoly, toPoly, &matrix );\r"
- "GraphicsImportSetMatrix( gi, &matrix );\r"
- "GraphicsImportDraw( gi );\r";
- else
- textPtr =
- "PolyToPolyMatrix( fromPoly, toPoly, &matrix );\r"
- "GraphicsImportSetMatrix( gi, &matrix );\r"
- "GraphicsImportSetGraphicsMode( gi, mode, color );\r"
- "GraphicsImportDraw( gi );\r";
- break;
- }
- textLength = strlen( textPtr );
-
- GetDocumentRects( theDocument, &textRect, nil, nil, nil );
-
- /*
- (*theDocument->docText)->txFont = kFontIDCourier;
- (*theDocument->docText)->txSize = 28;
- (*theDocument->docText)->txFace = bold;
- */
-
- TEDeactivate( theDocument->docText );
- TESetText( textPtr, textLength, theDocument->docText );
- InvalRect( &textRect );
-
- return noErr;
- }
-
- //
- // SetCodeVisible
- //
- void SetCodeVisible( DocumentPeek theDocument, Boolean visible )
- {
- WindowPtr window = (WindowPtr) theDocument;
-
- theDocument->docCodeVisible = visible;
- InvalRect( &window->portRect );
- }
-
- //
- // SetMatrixVisible
- //
- void SetMatrixVisible( DocumentPeek theDocument, Boolean visible )
- {
- WindowPtr window = (WindowPtr) theDocument;
-
- theDocument->docMatrixVisible = visible;
- InvalRect( &window->portRect );
- }
-
- //
- // SetFourPointsVisible
- //
- void SetFourPointsVisible( DocumentPeek theDocument, Boolean visible )
- {
- WindowPtr window = (WindowPtr) theDocument;
-
- theDocument->docFourPointsVisible = visible;
- InvalRect( &window->portRect );
- }
-
- //
- // DrawMatrix
- //
- OSErr DrawMatrix( DocumentPeek theDocument )
- {
- WindowPtr window = (WindowPtr) theDocument;
- MatrixRecord matrix = theDocument->docMatrix;
- Rect matrixRect;
- char buf[256];
- StringPtr pstr;
- FontInfo fi;
- short i, j;
- short top, left, right, bottom;
- short topGap = 10, bottomGap = 10, vertGap = 20;
- short leftGap = 10, rightGap = 20, horizGap = 20, fieldWidth;
-
- GetDocumentRects( theDocument, nil, nil, &matrixRect, nil );
-
- TextFont( kFontIDCourier );
- TextSize( 25 );
- TextFace( bold );
- //TextMode( srcCopy );
- GetFontInfo( &fi );
-
- top = matrixRect.top;
- left = matrixRect.left;
-
- topGap += fi.ascent;
- vertGap += fi.ascent + fi.descent;
- bottomGap += fi.descent;
- fieldWidth = 6 * CharWidth( 'x' );
- horizGap += fieldWidth;
-
- right = left + leftGap + 2 * horizGap + fieldWidth + rightGap;
- bottom = top + topGap + 2 * vertGap + bottomGap;
-
- PenSize( 3,3 );
-
- MoveTo( left + leftGap, top );
- LineTo( left, top );
- LineTo( left, bottom );
- LineTo( left + leftGap, bottom );
-
- MoveTo( right - rightGap, top );
- LineTo( right, top );
- LineTo( right, bottom );
- LineTo( right - rightGap, bottom );
-
- PenNormal();
-
- for( i = 0; i < 3; i++ ) {
- for( j = 0; j < 3; j++ ) {
- float_t f;
- MoveTo( left + leftGap + j * horizGap,
- top + topGap + i * vertGap );
- if( j < 2 ) {
- f = Fix2X( matrix.matrix[i][j] );
- sprintf( buf, "%3.1f", f );
- } else {
- f = Frac2X( matrix.matrix[i][j] );
- sprintf( buf, "%1.4f", f );
- }
- pstr = c2pstr( buf );
- Move( fieldWidth - StringWidth(pstr), 0 );
- DrawString( pstr );
- }
- }
-
- //InvalRect( &window->portRect );
-
- return noErr;
- }
-
- //
- // RedrawAfterMatrixChange
- //
- void RedrawAfterMatrixChange( DocumentPeek theDocument )
- {
- WindowPtr window = (WindowPtr) theDocument;
-
- SetPort( window );
- InvalRect( &window->portRect );
- }
-
- //
- // DrawWindow
- //
- OSErr
- DrawWindow(
- WindowPtr window )
- {
- DocumentPeek theDocument = (DocumentPeek) window;
- Rect textRect, drawRect, matrixRect, controlsRect;
- SetPort(window);
-
- GetDocumentRects( theDocument, &textRect, &drawRect, &matrixRect, &controlsRect );
-
- DrawBackground( theDocument );
-
- DrawGraphic( theDocument );
-
- if( theDocument->docMatrixVisible )
- DrawMatrix( theDocument );
-
- DrawControls( window );
-
- //EraseRect( &textRect );
- if( theDocument->docCodeVisible )
- TEUpdate( &textRect, theDocument->docText );
-
- //DrawGrowIcon(window);
-
- return noErr;
- } /*DrawWindow*/
-
- //
- // ResetDemoMatrix
- //
- void ResetDemoMatrix( DocumentPeek theDocument )
- {
- WindowPtr window = (WindowPtr) theDocument;
- SetPort( window );
-
- switch( theDocument->docDemo ) {
- case iTranslate:
- {
- Rect drawRect, naturalBounds, bounds;
- GetDocumentRects( theDocument, nil, &drawRect, nil, nil );
- GraphicsImportGetNaturalBounds( theDocument->docGrip, &naturalBounds );
- FitRectIntoRect( &naturalBounds, &drawRect, &bounds );
-
- theDocument->docTranslateBounds = bounds;
- }
- break;
- case iRotate:
- theDocument->docAngle = IntToFixed(90); // 0;
- break;
- case iDistort:
- {
- Rect drawRect, naturalBounds, bounds;
- GetDocumentRects( theDocument, nil, &drawRect, nil, nil );
- GraphicsImportGetNaturalBounds( theDocument->docGrip, &naturalBounds );
- FitRectIntoRect( &naturalBounds, &drawRect, &bounds );
-
- RectToFourPoints( &bounds, theDocument->docFourPoints );
- }
- break;
- }
- }
- //
- // DoMatrixMenu
- //
- void DoMatrixMenu( WindowPtr window, short item )
- {
- DocumentPeek theDocument = (DocumentPeek) window;
- SetPort( window );
-
- switch( item ) {
- case iTranslate:
- {
- Rect drawRect, naturalBounds, bounds;
- GetDocumentRects( theDocument, nil, &drawRect, nil, nil );
- GraphicsImportGetNaturalBounds( theDocument->docGrip, &naturalBounds );
- FitRectIntoRect( &naturalBounds, &drawRect, &bounds );
-
- theDocument->docTranslateBounds = bounds;
- }
- break;
- case iRotate:
- theDocument->docAngle = IntToFixed(90); // 0;
- break;
- case iDistort:
- {
- Rect drawRect, naturalBounds, bounds;
- GetDocumentRects( theDocument, nil, &drawRect, nil, nil );
- GraphicsImportGetNaturalBounds( theDocument->docGrip, &naturalBounds );
- FitRectIntoRect( &naturalBounds, &drawRect, &bounds );
-
- RectToFourPoints( &bounds, theDocument->docFourPoints );
- }
- break;
- case iShowCode:
- SetCodeVisible( theDocument, !theDocument->docCodeVisible );
- AdjustMenus();
- break;
- case iShowMatrix:
- SetMatrixVisible( theDocument, !theDocument->docMatrixVisible );
- AdjustMenus();
- break;
- case iShowPoints:
- SetFourPointsVisible( theDocument, !theDocument->docFourPointsVisible );
- AdjustMenus();
- break;
- }
-
- if( item <= iDistort ) {
- SetIdentityMatrix( &theDocument->docMatrix );
- theDocument->docDemo = item;
- ResetDemoMatrix( theDocument );
- SetUpDemoText( theDocument );
-
- RedrawAfterMatrixChange( theDocument );
- }
- }
-
- //
- // DoCompositionMenu
- //
- void DoCompositionMenu( WindowPtr window, short item )
- {
- DocumentPeek theDocument = (DocumentPeek) window;
- OSType theTypeListQTIF[1] = { kQTFileTypeQuickTimeImage };
- StandardFileReply theReply;
- FSSpec fileSpec;
- GraphicsImportComponent gi = 0;
- OSErr err;
-
-
- StandardGetFilePreview(
- nil,
- 1,
- theTypeListQTIF,
- &theReply );
-
- if( ! theReply.sfGood )
- return;
-
- fileSpec = theReply.sfFile;
- err = GetGraphicsImporterForFile( &fileSpec, &gi );
- if( err )
- return;
- if( 0 == gi )
- return;
-
- SetPort( window );
-
-
- switch( item ) {
- case iChangeImage:
- CloseComponent( theDocument->docGrip );
- theDocument->docGrip = gi;
- theDocument->docFSSpec = fileSpec;
- GraphicsImportSetGraphicsMode(
- theDocument->docGrip,
- theDocument->docTransferMode,
- &theDocument->docOpColor );
- ResetDemoMatrix( theDocument );
- break;
- case iChangeBackground:
- CloseComponent( theDocument->docGripBackground );
- theDocument->docGripBackground = gi;
- break;
- }
-
- RedrawAfterMatrixChange( theDocument );
- }
-
- //
- // DoTransferModeMenu
- //
- void DoTransferModeMenu( WindowPtr window, short item )
- {
- DocumentPeek theDocument = (DocumentPeek) window;
- SetPort( window );
-
- if( iChooseOpColor == item )
- {
- Point where = { 0,0 };
- RGBColor thePickedColor;
-
- if( GetColor( where, "\p", &theDocument->docOpColor, &thePickedColor) )
- {
- theDocument->docOpColor = thePickedColor;
- goto done;
- }
- else return;
- }
-
- theDocument->docTransferModeMenuItem = item;
-
- switch( theDocument->docTransferModeMenuItem )
- {
- default:
- case iDitherCopy:
- theDocument->docTransferMode = srcCopy + ditherCopy;
- break;
- case iSrcCopy:
- theDocument->docTransferMode = srcCopy;
- break;
- case iBlend:
- theDocument->docTransferMode = blend;
- break;
- case iTransparent:
- theDocument->docTransferMode = transparent;
- break;
- case iStraightAlpha:
- theDocument->docTransferMode = graphicsModeStraightAlpha + ditherCopy;
- break;
- case iPremulWhiteAlpha:
- theDocument->docTransferMode = graphicsModePreWhiteAlpha + ditherCopy;
- break;
- case iPremulBlackAlpha:
- theDocument->docTransferMode = graphicsModePreBlackAlpha + ditherCopy;
- break;
- case iStraightAlphaBlend:
- theDocument->docTransferMode = graphicsModeStraightAlphaBlend + ditherCopy;
- break;
- }
-
- done:
- GraphicsImportSetGraphicsMode(
- theDocument->docGrip,
- theDocument->docTransferMode,
- &theDocument->docOpColor );
-
- SetUpDemoText( theDocument );
- RedrawAfterMatrixChange( theDocument );
- }
-
- //
- // MakeDocumentControls
- //
- OSErr MakeDocumentControls( DocumentPeek doc, Rect *controlsRect )
- {
- WindowPtr window = (WindowPtr) doc;
-
- return noErr;
- }
-
- //
- // GetDocumentRects
- //
- void GetDocumentRects( DocumentPeek doc, Rect *outTextRect, Rect *outDrawRect, Rect *outMatrixRect, Rect *outControlsRect )
- {
- Rect r = doc->docWindow.port.portRect;
- short h_mid = (r.left+r.right)/2;
- short h_right = (r.left+r.right*3)/4;
- short v_high = (r.top*2+r.bottom)/3;
- short v_low = (r.top+r.bottom*2)/3;
-
- if( outTextRect ) {
- MacSetRect( outTextRect, r.left, r.top, r.right, v_high );
- InsetRect( outTextRect, 4, 4 );
- }
- if( outDrawRect ) {
- MacSetRect( outDrawRect, r.left, v_high, h_mid, r.bottom );
- InsetRect( outDrawRect, 4, 4 );
- }
- if( outMatrixRect ) {
- MacSetRect( outMatrixRect, h_mid, v_low, r.right, r.bottom );
- InsetRect( outMatrixRect, 4, 4 );
- }
- /*
- if( outControlsRect ) {
- MacSetRect( outControlsRect, hright, vmid, r.right, r.bottom );
- InsetRect( outControlsRect, 4, 4 );
- }
- */
- }
-
- //
- // OpenFSSpec
- //
- // Given a file, create a window and open a graphics importer for it.
- //
- OSErr
- OpenFSSpec( FSSpec *inFSSpec )
- {
- Ptr storage;
- WindowPtr theWindow = nil;
- DocumentPeek theDocument = nil;
- OSErr theError = noErr;
- Rect textRect, controlRect;
- RGBColor rgbcolor_Apple = { 0xefef,0x1e1e,0x1c1c };
-
- /* Build a document */
- storage = NewPtrClear(sizeof(DocumentRecord));
- if( nil == storage )
- return memFullErr;
-
- /* Create a window for the Image */
- theWindow = GetNewCWindow(kDemoWindowID, storage, (WindowPtr) -1);
- if( nil == theWindow )
- {
- DisposePtr( storage );
- return memFullErr;
- }
- theDocument = (DocumentPeek)theWindow;
-
- theDocument->docFSSpec = *inFSSpec;
- SetIdentityMatrix( &theDocument->docMatrix );
- theDocument->docDemo = iTranslate;
- theDocument->docCodeVisible = false;
- theDocument->docMatrixVisible = false;
- theDocument->docFourPointsVisible = false;
- theDocument->docTransferModeMenuItem = iDitherCopy;
- theDocument->docTransferMode = ditherCopy;
- theDocument->docOpColor = rgbcolor_Apple;
-
- /* Create a graphics import component. */
- SetPort( theWindow );
- theError = GetGraphicsImporterForFile( &theDocument->docFSSpec, &theDocument->docGrip );
-
- //•• set up something
- if( noErr == theError ) {
- GetDocumentRects( theDocument, &textRect, nil, nil, &controlRect );
-
- TextFont( kFontIDCourier );
- TextSize( 25 );
- TextFace( bold );
- theDocument->docText = TENew( &textRect, &textRect );
-
- if( nil == theDocument->docText )
- theError = memFullErr;
- }
-
- if( noErr == theError ) {
- TEDeactivate( theDocument->docText );
- SetUpDemoText( theDocument );
-
- theError = MakeDocumentControls( theDocument, &controlRect );
- }
-
- if( noErr == theError ) {
- ShowWindow( theWindow );
- DoMatrixMenu( theWindow, iTranslate );
- }
-
- if( noErr == theError )
- AdjustMenus();
-
- if( theError )
- {
- DoCloseWindow( theWindow );
- }
-
- return theError;
- } /* OpenFSSpec */
-
- //
- // DoCloseWindow
- //
- void DoCloseWindow(WindowPtr window)
- {
- DocumentPeek theDocument = (DocumentPeek) window;
- if( 0 != theDocument->docGrip )
- {
- CloseComponent( theDocument->docGrip );
- theDocument->docGrip = 0;
- }
-
- if( theDocument->docText )
- {
- TEDispose( theDocument->docText );
- theDocument->docText = 0;
- }
-
- MacCloseWindow(window);
- DisposePtr((Ptr) window);
-
- /* SetPort to a valid port for safety */
- if(FrontWindow())
- SetPort(FrontWindow());
- else
- {
- GrafPtr somePort;
- GetWMgrPort( &somePort );
- SetPort( somePort );
- }
-
- // Adjust the menus because we may now have no frontmost window.
- AdjustMenus();
- } /*DoCloseWindow*/
-
-
- //
- // DoOpen
- //
- // We require QT3.0; just ask for kQTFileTypeQuickTimeImage.
- //
- void
- DoOpen( void )
- {
- OSType theTypeListQTIF[1] = { kQTFileTypeQuickTimeImage };
- StandardFileReply theReply;
-
- StandardGetFilePreview(
- nil,
- 1,
- theTypeListQTIF,
- &theReply );
-
- if( theReply.sfGood )
- OpenFSSpec( &theReply.sfFile );
- }
- //
- // DoMenuCommand
- //
- void DoMenuCommand(long menuResult)
- {
- short menuID, menuItem;
- Str255 daName;
- WindowPtr window;
- GrafPtr savePort;
-
- window = FrontWindow();
- menuID = menuResult >> 16;
- menuItem = menuResult & 0xffff;
-
- switch ( menuID ) {
- case mApple:
- switch ( menuItem ) {
- case iAboutApp: /* bring up alert for About */
- Alert( kAboutAlertID, nil );
- break;
- default: /* all non-About items in this menu are DAs et al */
- GetPort(&savePort);
- GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
- OpenDeskAcc(daName);
- SetPort(savePort);
- break;
- }
- break;
-
- case mFile:
- switch ( menuItem ) {
- case iOpen:
- DoOpen();
- break;
- case iClose:
- DoCloseWindow(FrontWindow());
- break;
- case iQuit:
- Terminate();
- break;
- }
- break;
-
- case mEdit:
- break;
-
- case mMatrix:
- DoMatrixMenu( window, menuItem );
- break;
-
- case mComposition:
- DoCompositionMenu( window, menuItem );
- break;
-
- case mTransferMode:
- DoTransferModeMenu( window, menuItem );
- break;
-
- }
- HiliteMenu(0); /* unhighlight what MenuSelect (or MenuKey) hilited */
- } /*DoMenuCommand*/
-
- //
- // AdjustMenus
- //
- void AdjustMenus(void)
- {
- WindowPtr window;
- DocumentPeek theDocument;
- MenuHandle menu;
- short i;
-
- window = FrontWindow();
- theDocument = (DocumentPeek) window;
-
- menu = GetMenuHandle(mFile);
- if ( window != nil )
- {
- EnableItem(menu, iClose);
- }
- else
- {
- DisableItem(menu, iClose);
- }
-
- menu = GetMenuHandle(mEdit);
- DisableItem(menu, iUndo);
- DisableItem(menu, iCut);
- DisableItem(menu, iCopy);
- DisableItem(menu, iPaste);
- DisableItem(menu, iClear);
-
- menu = GetMenuHandle(mMatrix);
- if ( window != nil )
- {
- EnableItem(menu, 0);
- CheckItem(menu, iTranslate, iTranslate == theDocument->docDemo );
- CheckItem(menu, iRotate, iRotate == theDocument->docDemo );
- CheckItem(menu, iDistort, iDistort == theDocument->docDemo );
- CheckItem(menu, iShowCode, theDocument->docCodeVisible );
- CheckItem(menu, iShowMatrix, theDocument->docMatrixVisible );
- CheckItem(menu, iShowPoints, theDocument->docFourPointsVisible );
- }
- else
- {
- DisableItem(menu, 0);
- }
-
- menu = GetMenuHandle(mComposition);
- if ( window != nil )
- EnableItem(menu, 0);
- else
- DisableItem(menu, 0);
-
- menu = GetMenuHandle(mTransferMode);
- if ( window != nil )
- {
- EnableItem(menu, 0);
- for( i = iDitherCopy; i <= iStraightAlphaBlend; i++ )
- CheckItem(menu, i, i == theDocument->docTransferModeMenuItem );
- }
- else
- {
- DisableItem(menu, 0);
- }
-
- DrawMenuBar();
- } /*AdjustMenus*/
-
- //
- // GetSleep
- //
- unsigned long GetSleep(void)
- {
- long sleep;
- sleep = 60;
- return sleep;
- } /*GetSleep*/
-
- //
- // DoKeyDown
- //
- void DoKeyDown(EventRecord *event)
- {
- } /*DoKeyDown*/
-
- //
- // DoContentClick
- //
- void DoContentClick(WindowPtr window,EventRecord *event)
- {
- DocumentPeek theDocument = (DocumentPeek)window;
- switch( theDocument->docDemo ) {
- case iTranslate:
- DoTranslateClick( theDocument, event );
- break;
- case iRotate:
- DoRotateClick( theDocument, event );
- break;
- case iDistort:
- DoDistortClick( theDocument, event );
- break;
- }
- } /*DoContentClick*/
-
-
- //
- // DoActivate
- //
- void DoActivate(WindowPtr window, Boolean becomingActive)
- {
- } /*DoActivate*/
-
- //
- // DoUpdate
- //
- void DoUpdate(WindowPtr window)
- {
- SetPort( window );
- BeginUpdate(window); /* this sets up the visRgn */
- if ( ! EmptyRgn(window->visRgn) ) {
- EraseRect(&window->portRect);
- DrawWindow(window);
- }
- EndUpdate(window);
- } /*DoUpdate*/
-
- //
- // DoZoomWindow
- //
- void DoZoomWindow(WindowPtr window,short part)
- {
- SetPort(window);
- EraseRect(&window->portRect);
- ZoomWindow(window, part, window == FrontWindow());
- } /* DoZoomWindow */
-
- //
- // DoGrowWindow
- //
- void DoGrowWindow(WindowPtr window,EventRecord *event)
- {
- long growResult;
- Rect tempRect;
- DocumentPeek theDocument = (DocumentPeek) window;
-
- SetRect( &tempRect, 64, 64, 9999, 9999 );
- growResult = GrowWindow(window, event->where, &tempRect);
- /* see if it really changed size */
- if ( growResult != 0 ) {
- SetPort( window );
- SizeWindow(window, LoWrd(growResult), HiWrd(growResult), true);
- }
- } /* DoGrowWindow */
-
- //
- // GetGlobalMouse
- //
- void GetGlobalMouse(Point *mouse)
- {
- EventRecord event;
-
- OSEventAvail(0, &event);
- *mouse = event.where;
- } /*GetGlobalMouse*/
-
- //
- // ShiftKeyIsDown
- //
- Boolean ShiftKeyIsDown()
- {
- EventRecord event;
-
- OSEventAvail(0, &event);
- return 0 != (event.modifiers & shiftKey);
- } /*GetGlobalMouse*/
-
-
- //
- // AdjustCursor
- //
- void AdjustCursor(Point mouse,RgnHandle region)
- {
- if( ! gInBackground ) {
- SetCursor(&qd.arrow);
- }
- } /*AdjustCursor*/
-
- //
- // DoIdle
- //
- void DoIdle()
- {
-
- }
-
- //
- // DoEvent
- //
- void DoEvent(EventRecord *event)
- {
- short part;
- WindowPtr window;
- short key;
-
- switch ( event->what ) {
- case nullEvent:
- DoIdle();
- break;
- case mouseDown:
- part = FindWindow(event->where, &window);
- switch ( part ) {
- case inMenuBar: /* process a mouse menu command (if any) */
- AdjustMenus(); /* bring ’em up-to-date */
- DoMenuCommand(MenuSelect(event->where));
- break;
- case inSysWindow: /* let the system handle the mouseDown */
- SystemClick(event, window);
- break;
- case inContent:
- if ( window != FrontWindow() ) {
- SelectWindow(window);
- } else
- DoContentClick(window, event);
- break;
- case inDrag: /* pass screenBits.bounds to get all gDevices */
- DragWindow(window, event->where, &qd.screenBits.bounds);
- break;
- case inGoAway:
- if ( TrackGoAway(window, event->where) )
- DoCloseWindow(window);
- break;
- case inGrow:
- DoGrowWindow(window, event);
- break;
- case inZoomIn:
- case inZoomOut:
- if ( TrackBox(window, event->where, part) )
- DoZoomWindow(window, part);
- break;
- }
- break;
- case keyDown:
- case autoKey: /* check for menukey equivalents */
- key = event->message & charCodeMask;
- if ( event->modifiers & cmdKey ) { /* Command key down */
- if ( event->what == keyDown ) {
- AdjustMenus(); /* enable/disable/check menu items properly */
- DoMenuCommand(MenuKey(key));
- }
- } else
- DoKeyDown(event);
- break;
- case activateEvt:
- DoActivate((WindowPtr) event->message, (event->modifiers & activeFlag) != 0);
- break;
- case updateEvt:
- DoUpdate((WindowPtr) event->message);
- break;
- case osEvt:
- switch ((event->message >> 24) & 0x0FF) { /* high byte of message */
- case mouseMovedMessage:
- DoIdle();
- break;
- case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- gInBackground = (event->message & resumeFlag) == 0;
- DoActivate(FrontWindow(), !gInBackground);
- break;
- }
- break;
- /*case kHighLevelEvent:
- DoAppleEvent( *event );
- break;*/
- }
- } /*DoEvent*/
-
- //====================================================================================
- //
- // AppleEvent handling.
- //
-
- //
- // DoAppleEvent
- //
- void
- DoAppleEvent(
- EventRecord theEvent )
- {
- OSErr err;
-
- // should check for your own event message types here - if you have any
-
- err = AEProcessAppleEvent(&theEvent);
- }
-
- //
- // DoAEOpenApp
- //
- pascal OSErr
- DoAEOpenApp(
- const AppleEvent * message,
- const AppleEvent * reply,
- long refcon )
- {
- return noErr;
- }
-
- //
- // DoAEOpenDocument
- //
- pascal OSErr
- DoAEOpenDocument(
- const AppleEvent * message,
- const AppleEvent * reply,
- long refcon )
- {
- long index;
- long itemsInList;
- AEKeyword keywd;
- OSErr err;
- OSErr ignoreErr;
- AEDescList docList;
- long actSize;
- DescType typeCode;
- FSSpec theFSSpec;
-
- /*open the specified documents*/
-
- docList.dataHandle = nil;
-
- err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
-
- if (err==noErr)
- {
- err = AECountItems( &docList, &itemsInList) ;
- }
- else
- {
- itemsInList = 0;
- }
-
- for (index = 1; index <= itemsInList; index++)
- if (err==noErr)
- {
- err = AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
- (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize ) ;
- if (err==noErr)
- {
- OpenFSSpec( &theFSSpec );
- }
- }
-
- if (docList.dataHandle)
- {
- ignoreErr = AEDisposeDesc(&docList);
- }
-
- return err;
- }
-
- //
- // DoAEQuit
- //
- pascal OSErr
- DoAEQuit(
- const AppleEvent * message,
- const AppleEvent * reply,
- long refcon )
- {
- Terminate();
- return noErr;
- }
-
-
- //
- // DoAEPrintDocuments
- //
- pascal OSErr
- DoAEPrintDocuments(
- const AppleEvent * message,
- AppleEvent * reply,
- long refcon )
- {
- return errAEEventFailed;
- } /* DoPrintDocuments */
-
-
- //
- // InitAppleEvents
- //
- void
- InitAppleEvents(void)
- {
- const long noRefCon = 0;
- OSErr aevtErr;
-
- aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoAEOpenApp), noRefCon, false) ;
- aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(DoAEOpenDocument), noRefCon, false) ;
- aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(DoAEPrintDocuments), noRefCon, false) ;
- aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(DoAEQuit), noRefCon, false) ;
- }
-
- //====================================================================================
-
- /* ******************************** PolyToPolyMatrix ******************************** */
-
- // PolyToPolyMatrix takes two polygons and generates a matrix mapping from one to the other.
- // The two polygons are stored as arrays of longs; the first long is the number of points
- // in the polygon; the points follow as FixedPoints: x, then y.
- // Both polygons should have the same number of points.
- // Polygons with more than 4 points will be treated as though they had only 4 points.
-
- static long MultiplyDivide(long source, long multiplier, long divisor)
- {
- wide temp;
- CompMul(source, multiplier, &temp);
- return CompDiv(&temp, divisor, 0);
- }
-
- static wide *VectorMultiply(register long count, register const long *vector1, register long step1,
- register const long *vector2, register long step2, register wide *dot)
- {
- short flags = count >= 0;
- dot->hi = dot->lo = 0;
- if (!flags)
- count = -count;
- while (count--)
- { wide temp;
- if (flags ^= 2)
- {
- CompMul(*vector1, *vector2, &temp);
- CompAdd(&temp, dot);
- }
- else
- {
- CompMul(*vector1, *vector2, &temp);
- CompSub(&temp, dot);
- }
- vector1 += step1;
- vector2 += step2;
- }
- return dot;
- }
-
- static long VectorMultiplyDivide(long count, const long *vector1, long step1, const long *vector2, long step2, long scalar)
- {
- wide temp;
- return CompDiv(VectorMultiply(count, vector1, step1, vector2, step2, &temp), scalar, 0);
- }
-
- // ------------------------------------------------------------------------------------------
- static unsigned long MyWideSquareRoot(const wide *src)
- {
- { register unsigned long srchi=src->hi;
- register unsigned long srclo=src->lo;
- register unsigned long root=0;
- register unsigned long left=0;
- register unsigned long delta=0;
- unsigned short i, lefthi, deltahi;
-
- i = 16; /* compute the first 16 bits */
- while (i--)
- { left = left << 2 | srchi >> 30;
- srchi <<= 2;
- root <<= 1;
- delta = (root << 1) + 1;
- if (left >= delta)
- { left -= delta;
- root += 1;
- }
- }
-
- i = 14; /* compute the next 14 bits */
- while(i--)
- { left = left << 2 | srclo >> 30;
- srclo <<= 2;
- root <<= 1;
- delta = (root << 1) + 1;
- if (left >= delta)
- { left -= delta;
- root += 1;
- }
- }
-
- i = 2; /* compute the last two bits */
- while (i--)
- { lefthi = left >> 30;
- left = left << 2 | srclo >> 30;
- srclo <<= 2;
- root <<= 1;
- deltahi = root >> 31;
- delta = (root << 1) + 1;
- if (lefthi > deltahi || lefthi == deltahi && left >= delta)
- { if (left < delta)
- lefthi -= deltahi + 1;
- else
- lefthi -= deltahi;
- left -= delta;
- root += 1;
- }
- }
-
- if (lefthi || left > root) /* round */
- if (root + 1) /* don't exceed 32 bits */
- root += 1;
-
- return root;
- }
- }
-
- static unsigned long Magnitude(long deltaX, long deltaY)
- {
- wide temp1, temp2;
-
- if (deltaX == 0)
- return deltaY < 0 ? -deltaY : deltaY;
- if (deltaY == 0)
- return deltaX < 0 ? -deltaX : deltaX;
- CompMul(deltaX, deltaX, &temp1);
- CompMul(deltaY, deltaY, &temp2);
- CompAdd(&temp1, &temp2);
- return MyWideSquareRoot(&temp2);
- }
-
- static void PolyToPoint(const Fixed *poly, FixedPoint *pt)
- {
- register Fixed x = fixed1, y = fixed1;
- FixedPoint pt1, pt2;
-
- if (poly[0] > 1)
- { pt1.x = poly[3] - poly[1];
- pt1.y = poly[4] - poly[2];
- y = Magnitude(pt1.x, pt1.y);
- switch (poly[0])
- { case 2:
- break;
- case 3:
- pt2.x = poly[2] - poly[6];
- pt2.y = poly[5] - poly[1];
- goto calcX;
- default:
- pt2.x = poly[2] - poly[8];
- pt2.y = poly[7] - poly[1];
- calcX:
- x = VectorMultiplyDivide(2, &pt1.x, 1, &pt2.x, 1, y);
- break;
- }
- }
- pt->x = x;
- pt->y = y;
- }
-
- static void Map1Pt(const FixedPoint *source, Fixed *dest)
- {
- register Fixed *destPtr = dest;
- register const FixedPoint *srcPt = source;
- *destPtr++ = fixed1;
- *destPtr++ = 0;
- *destPtr++ = 0;
- *destPtr++ = 0;
- *destPtr++ = fixed1;
- *destPtr++ = 0;
- *destPtr++ = srcPt[0].x;
- *destPtr++ = srcPt[0].y;
- *destPtr = fract1;
- }
-
- static void Map2Pt(const FixedPoint *source, Fixed *dest, Fixed scale)
- {
- register Fixed *destPtr = dest;
- register const FixedPoint *srcPt = source;
- *destPtr++ = FixDiv(srcPt[1].y - srcPt[0].y, scale);
- *destPtr++ = FixDiv(srcPt[0].x - srcPt[1].x, scale);
- *destPtr++ = 0;
- *destPtr++ = FixDiv(srcPt[1].x - srcPt[0].x, scale);
- *destPtr++ = FixDiv(srcPt[1].y - srcPt[0].y, scale);
- *destPtr++ = 0;
- *destPtr++ = srcPt[0].x;
- *destPtr++ = srcPt[0].y;
- *destPtr = fract1;
- }
-
- static void Map3Pt(const FixedPoint *source, Fixed *dest, Fixed scaleX, Fixed scaleY)
- {
- register Fixed *destPtr = dest;
- register const FixedPoint *srcPt = source;
- *destPtr++ = FixDiv(srcPt[2].x - srcPt[0].x, scaleX);
- *destPtr++ = FixDiv(srcPt[2].y - srcPt[0].y, scaleX);
- *destPtr++ = 0;
- *destPtr++ = FixDiv(srcPt[1].x - srcPt[0].x, scaleY);
- *destPtr++ = FixDiv(srcPt[1].y - srcPt[0].y, scaleY);
- *destPtr++ = 0;
- *destPtr++ = srcPt[0].x;
- *destPtr++ = srcPt[0].y;
- *destPtr = fract1;
- }
-
- static void Map4Pt(const FixedPoint *source, Fixed *dest, Fixed scaleX, Fixed scaleY)
- {
- register Fixed *destPtr = dest;
- register const FixedPoint *srcPt = source;
- Fract a1, a2;
- Fixed x0, y0, x1, y1, x2, y2;
-
- x0 = srcPt[2].x - srcPt[0].x;
- y0 = srcPt[2].y - srcPt[0].y;
- x1 = srcPt[2].x - srcPt[1].x;
- y1 = srcPt[2].y - srcPt[1].y;
- x2 = srcPt[2].x - srcPt[3].x;
- y2 = srcPt[2].y - srcPt[3].y;
-
- // check if abs(x2) > abs(y2)
- if ( x2 > 0 ? y2 > 0 ? x2 > y2 : x2 > -y2 : y2 > 0 ? -x2 > y2 : x2 < y2)
- a1 = FracDiv(MultiplyDivide(x0 - x1, y2, x2) - y0 + y1, MultiplyDivide(x1, y2, x2) - y1);
- else
- a1 = FracDiv(x0 - x1 - MultiplyDivide(y0 - y1, x2, y2), x1 - MultiplyDivide(y1, x2, y2));
-
- // check if abs(x1) > abs(y1)
- if ( x1 > 0 ? y1 > 0 ? x1 > y1 : x1 > -y1 : y1 > 0 ? -x1 > y1 : x1 < y1)
- a2 = FracDiv(y0 - y2 - MultiplyDivide(x0 - x2, y1, x1), y2 - MultiplyDivide(x2, y1, x1));
- else
- a2 = FracDiv(MultiplyDivide(y0 - y2, x1, y1) - x0 + x2, MultiplyDivide(y2, x1, y1) - x2);
-
- *destPtr++ = FixDiv(FracMul(a2, srcPt[3].x) + srcPt[3].x - srcPt[0].x, scaleX);
- *destPtr++ = FixDiv(FracMul(a2, srcPt[3].y) + srcPt[3].y - srcPt[0].y, scaleX);
- *destPtr++ = FixDiv(a2, scaleX);
- *destPtr++ = FixDiv(FracMul(a1, srcPt[1].x) + srcPt[1].x - srcPt[0].x, scaleY);
- *destPtr++ = FixDiv(FracMul(a1, srcPt[1].y) + srcPt[1].y - srcPt[0].y, scaleY);
- *destPtr++ = FixDiv(a1, scaleY);
- *destPtr++ = srcPt[0].x;
- *destPtr++ = srcPt[0].y;
- *destPtr = fract1;
- }
-
- void PolyToPolyMatrix(const Fixed *source, const Fixed *dest, MatrixRecord *map)
- {
- FixedPoint tempPt;
- MatrixRecord tempMap;
- long vectors = source[0];
-
- PolyToPoint(source, &tempPt);
- switch (vectors)
- { case 0:
- SetIdentityMatrix(map);
- break;
- case 1:
- Map1Pt((FixedPoint*)&source[1], &tempMap.matrix[0][0]);
- InverseMatrix(&tempMap, map);
- Map1Pt((FixedPoint*)&dest[1], &tempMap.matrix[0][0]);
- goto mapMap;
- case 2:
- Map2Pt((FixedPoint*)&source[1], &tempMap.matrix[0][0], tempPt.y);
- InverseMatrix(&tempMap, map);
- Map2Pt((FixedPoint*)&dest[1], &tempMap.matrix[0][0], tempPt.y);
- goto mapMap;
- case 3:
- Map3Pt((FixedPoint*)&source[1], &tempMap.matrix[0][0], tempPt.x, tempPt.y);
- InverseMatrix(&tempMap, map);
- Map3Pt((FixedPoint*)&dest[1], &tempMap.matrix[0][0], tempPt.x, tempPt.y);
- goto mapMap;
- default:
- Map4Pt((FixedPoint*)&source[1], &tempMap.matrix[0][0], tempPt.x, tempPt.y);
- InverseMatrix(&tempMap, map);
- Map4Pt((FixedPoint*)&dest[1], &tempMap.matrix[0][0], tempPt.x, tempPt.y);
- mapMap:
- ConcatMatrix(&tempMap, map);
- break;
- }
- }
-
- /* ******************************************************************************* */
-
- //====================================================================================
-
- //
- // EventLoop
- //
- void EventLoop(void)
- {
- Boolean gotEvent;
- EventRecord event;
- Point mouse;
-
- do {
- GetGlobalMouse(&mouse);
- gotEvent = WaitNextEvent(everyEvent, &event, GetSleep(), nil);
- if ( gotEvent ) {
- /* make sure we have the right cursor before handling the event */
- AdjustCursor(event.where, nil);
- DoEvent(&event);
- }
- else
- {
- DoIdle(); /* perform idle tasks when it’s not our event */
- }
- } while ( !gExitFlag );
-
- } /*EventLoop*/
-
- //
- // Terminate
- //
- void Terminate(void)
- {
- gExitFlag = true;
- } /*Terminate*/
-
-
- //
- // Initialize
- //
- OSErr Initialize(void)
- {
- Handle menuBar;
- EventRecord event;
- short count;
- long theResult = 0;
- OSErr theError = noErr;
-
- gInBackground = false;
-
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- for (count = 1; count <= 3; count++)
- EventAvail(everyEvent, &event);
-
- theError = Gestalt( gestaltQuickTimeVersion, &theResult );
- if( theError ) return theError;
- if( theResult < 0x03000000 ) return userCanceledErr;
-
- menuBar = GetNewMBar(kMenuBarID); /* read menus into menu bar */
- if( !menuBar ) return memFullErr;
- SetMenuBar(menuBar); /* install menus */
- DisposeHandle(menuBar);
- AppendResMenu(GetMenuHandle(mApple), 'DRVR'); /* add DA names to Apple menu */
- AdjustMenus();
-
- InitAppleEvents();
- SetCursor(&qd.arrow);
-
- return noErr;
- } /*Initialize*/
-
-
- //
- // main
- //
- void main(void)
- {
- MaxApplZone(); /* expand the heap so code segments load at the top */
-
- if( noErr == Initialize() ) /* initialize the program */
- {
- EventLoop(); /* call the main event loop */
- }
- }
-